<template>
  <el-dialog
      :title="!dataForm.adId ? '新增' : '修改'"
      :close-on-click-modal="false"
      @close="formReset"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="管理员账号" prop="adUserName">
        <el-input v-model="dataForm.adUserName" placeholder="管理员账号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="adName">
        <el-input v-model="dataForm.adName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="adPhone">
        <el-input v-model="dataForm.adPhone" placeholder="电话号码"></el-input>
      </el-form-item>
      <el-form-item label="签名" prop="adSignature">
        <file-upload upload-url="http://localhost:8088/reportApproval/admin/fileUplod"
                     :value="dataForm.adSignature" @change="(val) => dataForm.adSignature = val"></file-upload>
      </el-form-item>
<!--      <el-form-item label="是否为管理员" prop="administratorFlag">-->
<!--          <el-switch-->
<!--              style="display: block"-->
<!--              v-model="dataForm.administratorFlag"-->
<!--              active-color="#13ce66"-->
<!--              active-value="0"-->
<!--              inactive-value="1"-->
<!--              inactive-color="#ff4949"-->
<!--              active-text="管理员"-->
<!--              inactive-text="用户"-->
<!--              @change="handleStatusChange(dataForm.administratorFlag)">-->
<!--          </el-switch>-->
<!--      </el-form-item>-->
      <el-form-item label="角色">
        <el-checkbox-group v-model="roleIds">
          <el-checkbox label="admin">系统管理员</el-checkbox>
          <el-checkbox label="approver">批准人</el-checkbox>
          <el-checkbox label="auditor">审核人</el-checkbox>
          <el-checkbox label="principal">项目负责人</el-checkbox>
          <el-checkbox label="checker">报告校核人</el-checkbox>
          <el-checkbox label="writer">用户</el-checkbox>
          <el-checkbox label="recorder">记录人</el-checkbox>
          <el-checkbox label="checkerPerson">检测人</el-checkbox>
          <el-checkbox label="sampler">抽样人</el-checkbox>
          <el-checkbox label="departmentHead">单位负责人</el-checkbox>
          <el-checkbox label="participant">参加人</el-checkbox>
          <el-checkbox label="technicalHead">技术负责人</el-checkbox>
          <el-checkbox label="chief">站长</el-checkbox>
          <el-checkbox label="aggregators">汇总人</el-checkbox>
          <el-checkbox label="companyHead">企业负责人</el-checkbox>
          <el-checkbox label="transferPerson">移交人</el-checkbox>
          <el-checkbox label="receiver">接收人</el-checkbox>

        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload.vue";

export default {
  components: {FileUpload},
  data() {
    return {
      visible: false,
      dataForm: {
        adId: 0,
        adUserName: '',
        adPassWord: '',
        adName: '',
        adPhone: '',
        adLoginAttempts: '',
        adLastLogin: '',
        adIco: '',
        adSignature: '',
        adStatus: '',
        adCreated: '',
        adUpdated: '',
        administratorFlag: ''
      },
      roleIds: [],
      fileList: [{name: '', url: 'http://127.0.0.1:8088/'}],
      dataRule: {
        adUserName: [
          {required: true, message: '管理员账号不能为空', trigger: 'blur'}
        ],
        adPassWord: [
          {required: true, message: '管理员密码不能为空', trigger: 'blur'}
        ],
        adName: [
          {required: true, message: '姓名不能为空', trigger: 'blur'}
        ],
        adPhone: [
          {required: true, message: '电话号码不能为空', trigger: 'blur'}
        ],
        adLoginAttempts: [
          {required: true, message: '尝试登录次数不能为空', trigger: 'blur'}
        ],
        adLastLogin: [
          {required: true, message: '最后登录时间不能为空', trigger: 'blur'}
        ],
        adIco: [
          {required: true, message: '头像不能为空', trigger: 'blur'}
        ],
        adSignature: [
          {required: true, message: '签名不能为空', trigger: 'blur'}
        ],
        adStatus: [
          {required: true, message: '管理员状态不能为空', trigger: 'blur'}
        ],
        adCreated: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],
        adUpdated: [
          {required: true, message: '修改时间不能为空', trigger: 'blur'}
        ],
        administratorFlag: [
          {required: true, message: '是否为超级管理员: 0 不是，1是不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // handleStatusChange(value){//dataForm.administratorFlag
    //   this.dataForm.administratorFlag = value
    // },
    formReset() {
      this.dataForm = {
        adId: 0,
            adUserName: '',
            adPassWord: '',
            adName: '',
            adPhone: '',
            adLoginAttempts: '',
            adLastLogin: '',
            adIco: '',
            adSignature: '',
            adStatus: '',
            adCreated: '',
            adUpdated: '',
            administratorFlag: ''
      };
      this.roleIds = [];
    },
    getData(file) {
      console.log(file)
      if (file.response.code === 0) {
        this.dataForm.adSignature = file.response.data.url
        this.fileList.push(file)
      }
    },
    submitUpload() {
      // console.log(file)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file.response);
    },
    getHeaders() {
      if (typeof sessionStorage !== 'undefined' && sessionStorage.getItem('token')) {
        return {Authorization: `Bearer ${sessionStorage.getItem('token')}`};
      }
      return {};
    },
    init(id) {
      this.dataForm.adId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.adId) {
          this.$http({
            url: this.$http.adornUrl(`/reportApproval/admin/info/${this.dataForm.adId}`),
            method: 'post',
            params: this.$http.adornParams()
          }).then((data) => {
            if (data && data.code === 0) {
              this.dataForm.adUserName = data.admin.adUserName
              this.dataForm.adPassWord = data.admin.adPassWord
              this.dataForm.adName = data.admin.adName
              this.dataForm.adPhone = data.admin.adPhone
              this.dataForm.adLoginAttempts = data.admin.adLoginAttempts
              this.dataForm.adLastLogin = data.admin.adLastLogin
              this.dataForm.adIco = data.admin.adIco
              this.dataForm.adSignature = data.admin.adSignature
              this.dataForm.adStatus = data.admin.adStatus
              this.dataForm.adCreated = data.admin.adCreated
              this.dataForm.adUpdated = data.admin.adUpdated
              // this.dataForm.administratorFlag = data.admin.administratorFlag
              this.roleIds = data.admin.roleIds
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/reportApproval/admin/${!this.dataForm.adId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'adId': this.dataForm.adId || undefined,
              'adUserName': this.dataForm.adUserName,
              'adPassWord': this.dataForm.adPassWord,
              'adName': this.dataForm.adName,
              'adPhone': this.dataForm.adPhone,
              'adLoginAttempts': this.dataForm.adLoginAttempts,
              'adLastLogin': this.dataForm.adLastLogin,
              'adIco': this.dataForm.adIco,
              'adSignature': this.dataForm.adSignature,
              'adStatus': this.dataForm.adStatus,
              'adCreated': this.dataForm.adCreated,
              'adUpdated': this.dataForm.adUpdated,
              // 'administratorFlag': this.dataForm.administratorFlag,
              'roleIds': this.roleIds
            })
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
